//
//  localitySongsViewController.m
//  Gegeda_music_MY
//
//  Created by lanou3g on 15/11/3.
//  Copyright © 2015年 周莹. All rights reserved.
//

#import "localitySongsViewController.h"
#import "ContextMentCell.h"
#import "YALContextMenuTableView.h"
#import "YALContextMenuCell.h"
#import "songsViewController.h"
#import "singersViewController.h"
#import "popViewController.h"
#import "folderViewController.h"

#define kScreenHeight ([UIScreen mainScreen].bounds.size.height)
#define kScreenWidth ([UIScreen mainScreen].bounds.size.width)
static NSString *const menuCellIdentifier = @"rotationCell";

@interface localitySongsViewController ()<UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate, YALContextMenuTableViewDelegate>
//@property(nonatomic, strong)UISegmentedControl *topSegCon;
//@property(nonatomic, strong)UIScrollView *bigScrollerView;
//@property(nonatomic, strong)UITableView *tableView1;
//@property(nonatomic, strong)UITableView *tableView2;
//@property(nonatomic, strong)UITableView *tableView3;
//@property(nonatomic, strong)UITableView *tableView4;
@property(nonatomic,strong)UIView *topView;
@property(nonatomic,strong)UIScrollView *bigScrollView;
@property(nonatomic,strong)UILabel *bottomLabel;
@property(nonatomic,assign)NSInteger index;



//菜单栏
@property (nonatomic, strong) YALContextMenuTableView* contextMenuTableView;
@property (nonatomic, strong) NSArray *menuTitles;
@property (nonatomic, strong) NSArray *menuIcons;


@end

@implementation localitySongsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.title = @"本地音乐";
    self.view.backgroundColor = [[UIColor alloc] initWithRed:255.0/255 green:222.0/255 blue:249.0/255 alpha:1.0];
    //左按钮
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 30, 30);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"23.png"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    //右按钮
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(300, 0, 30, 30);
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"30.png"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    //[self createMiddleScrollView];
    //[self createTopSegColl];
    //滚动图
    self.bigScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 52, self.view.frame.size.width,self.view.frame.size.height + 20)];
    self.bigScrollView.pagingEnabled = YES;
//    self.bigScrollView.backgroundColor = [UIColor blueColor];
    self.bigScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 4, 0);
    [self.view addSubview:self.bigScrollView];
    self.bigScrollView.delegate = self;

    
    self.contextMenuTableView.delegate = self;
    self.contextMenuTableView.dataSource = self;
    //self.tableView1.delegate = self;
    //self.tableView1.dataSource = self;
    
    self.navigationController.navigationBar.translucent = NO;
    [self creatTopScrollViewButton];
    
    [self setSmallControllers];
    
    [self creatSmallViews];
    
    [self initiateMenuOptions];
    
}
#pragma mark - 左button事件
-(void)leftButtonAction:(UIButton *)sender{
    [self.navigationController popToRootViewControllerAnimated:YES];

}


#pragma mark - 右菜单栏事件
-(BOOL)prefersStatusBarHidden{
    return YES;
}

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    [self.contextMenuTableView reloadData];
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self.contextMenuTableView updateAlongsideRotation];
}
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        //should be called after rotation animation completed
        [self.contextMenuTableView reloadData];
    }];
    [self.contextMenuTableView updateAlongsideRotation];

}

-(void)rightButtonAction:(UIButton *)sendr{
    // init YALContextMenuTableView tableView
    if (!self.contextMenuTableView) {
        self.contextMenuTableView = [[YALContextMenuTableView alloc]initWithTableViewDelegateDataSource:self];
        self.contextMenuTableView.animationDuration = 0.15;
        //optional - implement custom YALContextMenuTableView custom protocol
        self.contextMenuTableView.yalDelegate = self;
        
        //register nib
        UINib *cellNib = [UINib nibWithNibName:@"ContextMentCell" bundle:nil];
        [self.contextMenuTableView registerNib:cellNib forCellReuseIdentifier:menuCellIdentifier];
    }
    
    // it is better to use this method only for proper animation
    [self.contextMenuTableView showInView:self.navigationController.view withEdgeInsets:UIEdgeInsetsZero animated:YES];
    
   
}
#pragma mark - Local methods

- (void)initiateMenuOptions {
    self.menuTitles = @[@"",
                        @"升级音质",
                        @"扫描歌曲",
                        @"一键下载词图"];
    
    self.menuIcons = @[[UIImage imageNamed:@"35.png"],
                       [UIImage imageNamed:@"33.png"],
                       [UIImage imageNamed:@"34.png"],
                       [UIImage imageNamed:@"32.png"]];
}


#pragma mark - YALContextMenuTableViewDelegate

- (void)didDismissWithIndexPath:(NSIndexPath*)indexPath {
    NSLog(@"Menu dismissed with indexpath = %@", indexPath);
}

#pragma mark - UITableViewDataSource, UITableViewDelegate

- (void)tableView:(YALContextMenuTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView dismisWithIndexPath:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 65;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.menuTitles.count;
}

- (UITableViewCell *)tableView:(YALContextMenuTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ContextMentCell *cell = [tableView dequeueReusableCellWithIdentifier:menuCellIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [[UIColor alloc] initWithRed:255.0/255 green:222.0/255 blue:249.0/255 alpha:1.0];
    
    if (cell) {
        cell.menuTitleLabel.text = [self.menuTitles objectAtIndex:indexPath.row];
        cell.menuImageView.image = [self.menuIcons objectAtIndex:indexPath.row];
    }
    
    return cell;
}


#pragma mark - 创建滚动图
-(void)creatTopScrollViewButton
{
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    //  self.topView.contentSize = CGSizeMake( 8 * 80, 0);
    self.topView.backgroundColor = [[UIColor alloc] initWithRed:255.0/255 green:222.0/255 blue:249.0/255 alpha:1.0];
    NSArray *nameArray = @[@"我的歌单",@"歌手",@"专辑",@"文件夹"];
    
    for (int i = 0; i < nameArray.count; i++)
    {
        UIButton *nameButton = [[UIButton alloc]initWithFrame:CGRectMake(i * 102, 10, 100, 30)];
        //  nameButton.backgroundColor = [UIColor purpleColor];
        [self.topView addSubview:nameButton];
        [nameButton setTitle:nameArray[i] forState:UIControlStateNormal];
        nameButton.tag = i;
        // nameButton.backgroundColor = [UIColor brownColor];
        [nameButton addTarget:self action:@selector(buttonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    self.bottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 102, 1)];
    self.bottomLabel.backgroundColor = [UIColor blackColor];
    [self.topView addSubview:self.bottomLabel];
    
    [self.view addSubview:self.topView];
}

-(void)buttonDidClicked:(UIButton*)sender
{
    //NSLog(@"button");
    self.bigScrollView.contentOffset = CGPointMake(sender.tag * self.view.frame.size.width, 0);
    self.bottomLabel.frame = CGRectMake(sender.tag * 102, 40, 102, 1);
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.index = scrollView.contentOffset.x / 600;
//    NSLog(@"-------%f",scrollView.contentOffset.x);
//    NSLog(@"========%f",self.view.frame.size.width);
//    NSLog(@"%ld",self.index);
    [UIView animateWithDuration:0.1 animations:^{
        self.bottomLabel.frame = CGRectMake(self.index * 102, 40, 102, 1) ;
        
    } completion:^(BOOL finished) {
        
    }];
    
}
// // // // 关键一步
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    for (UIView *view in self.bigScrollView.subviews) {
        if ([view isMemberOfClass:[UIImageView class]]) {
            [view removeFromSuperview];
        }
    }
}

-(void)setSmallControllers
{
    songsViewController *songsVC = [[songsViewController alloc]init];
    [self addChildViewController:songsVC];
    
    singersViewController *singerVC = [[singersViewController alloc]init];
    [self addChildViewController:singerVC];
    
    popViewController *popVC = [[popViewController alloc]init];
    [self addChildViewController:popVC];
    
    folderViewController *folderVC = [[folderViewController alloc]init];
    [self addChildViewController:folderVC];
}

-(void)creatSmallViews
{
    songsViewController *firstVC = [self.childViewControllers firstObject];
    [self.bigScrollView addSubview:firstVC.view];
    // NSLog(@"%f", firstVC.view.frame.origin.y);
    firstVC.view.frame = CGRectMake(0, 0, self.view.frame.size.width, 620) ;
    
    singersViewController *secondVC = self.childViewControllers[1];
    [self.bigScrollView addSubview:secondVC.view];
    secondVC.view.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, 620);
    
    popViewController *thirdVC = self.childViewControllers[2];
    [self.bigScrollView addSubview:thirdVC.view];
    thirdVC.view.frame = CGRectMake(self.view.frame.size.width * 2, 0, self.view.frame.size.width, 620);
    
    folderViewController *fourthVC = self.childViewControllers[3];
    [self.bigScrollView addSubview:fourthVC.view];
    fourthVC.view.frame = CGRectMake(self.view.frame.size.width * 3, 0, self.view.frame.size.width, 620);
    
}


//#pragma mark - 创建选择栏
//-(void)createTopSegColl{
//    NSArray *segmentArray = [[NSArray alloc] initWithObjects:@"歌曲", @"歌手", @"专辑", @"文件夹", nil];
//    self.topSegCon = [[UISegmentedControl alloc] initWithItems:segmentArray];
//    self.topSegCon.frame = CGRectMake(0, 64, kScreenWidth, 50);
//    self.topSegCon.selectedSegmentIndex = 0;
//    //点击事件
//    [self.topSegCon addTarget:self action:@selector(segControlAction:) forControlEvents:UIControlEventValueChanged];
//    [self.view addSubview:self.topSegCon];
//    
//}
//
//#pragma mark - 选择栏事件
//-(void)segControlAction:(UISegmentedControl*)sender
//{
//    CGFloat x = (sender.selectedSegmentIndex) * 375;
//    [self.bigScroller setContentOffset:CGPointMake(x, 0) animated:YES];
//    
//}
//#pragma mark - 代理方法 - 实现换页
//-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//    NSInteger page = scrollView.contentOffset.x / 375;
//    self.topSegCon.selectedSegmentIndex = page;
//    NSLog(@"%ld",page);
//}
//
//#pragma mark - 创建中间scrollview
//-(void)createMiddleScrollView{
//    self.bigScroller = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    self.bigScroller.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 4, 0);
//    self.bigScroller.frame = CGRectMake(0, 114, kScreenWidth, kScreenHeight - 64 - 50);
//    self.bigScroller.pagingEnabled = YES;
//    self.bigScroller.delegate = self;
//    [self.view addSubview:self.bigScroller];
//}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
